package com.gpj.sqlbuilder.store.sql;

import com.gpj.sqlbuilder.annotations.ExcelOrder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className Table
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/3/26 15:00
 * @version 1.0
 **/
@Setter
@Getter
@NoArgsConstructor
public class Table {
    @ExcelOrder(value = 0, required = true)
    private String name;// 表名

    @ExcelOrder(value = 1, required = true)
    private String note;// 中文名

    @ExcelOrder(value = 2)
    private String schema;// schema

    @ExcelOrder(value = 3)
    private String encod = "CHARSET=utf8 COLLATE=utf8_bin";// 编码

    private List<Column> columns = new ArrayList();
    private Map<String, Column> columnMap = new HashMap<>();

    private Map<String, Index> indexs = new HashMap<>();

    private List<Data> datas = new ArrayList();

    public Column getColumn(String columnName) {
        if(columns != null && columns.size() > 0){
            if(columnMap != null && columnMap.size() == columns.size()){
                return columnMap.get(columnName);
            }else{
                columnMap = columns.stream()
                        .collect(Collectors.toMap(column -> column.getName(), column -> column));
                return columnMap.get(columnName);
            }
        }
        return null;
    }

}
